# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

from XiaChuFang.items import advertItem
from XiaChuFang import settings
import pymysql

class DBPipeline(object):
    def __init__(self):
        # 连接数据库
        self.connect = pymysql.connect(
            host=settings.MYSQL_HOST,
            db=settings.MYSQL_DBNAME,
            user=settings.MYSQL_USER,
            passwd=settings.MYSQL_PASSWD,
            charset='utf8',
            use_unicode=True)

        # 通过cursor执行增删查改
        self.cursor = self.connect.cursor();

class XiachufangPipeline(DBPipeline):
    def process_item(self,item,spider):
        pass

class CategorysPipeline(DBPipeline):
    def process_item(self, item, spider):
        self.cursor.execute("INSERT INTO  xiaChuFang_categroy (name,categroyId) values(%s,%s)",
                            (item['title'], item['url']))
        self.connect.commit()


class advetPipeline(DBPipeline):

    def process_item(self, item, spider):
        self.cursor.execute("INSERT INTO  user_operation_banner (title,image) values(%s,%s)",
                       (item['title'],item['image']))
        self.connect.commit()

